package com.sky.demo;

import com.sky.domain.User;

import java.sql.*;

/**
 * @author chengjy
 * @date 2023/4/29
 */
public class JdbcDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1、导包
        //2、加载驱动类
        String driverClass = "com.mysql.cj.jdbc.Driver";
        Class.forName(driverClass);
        //3、获取连接 connection 理解为一个Socket  java-mysql
        //3.1、设置连接用户信息
        String sql = "select * from user where id = ?";
        String url = "jdbc:mysql://localhost:3306/sky_take_out";
        String username = "root";
        String password = "1234";
        Connection connection = DriverManager.getConnection(url, username, password);
        //4、获取连接参数 statement  理解为一个io
        PreparedStatement ps = connection.prepareStatement(sql);
        //5、执行数据库操作
        ps.setInt(1, 1);
        ResultSet resultSet = ps.executeQuery();
        //5.1、解析返回值
        User user = new User();
        if (resultSet.next()) {

            user.setName(resultSet.getString("name"));
            user.setId(resultSet.getLong("id"));
            user.setAvatar(resultSet.getString("avatar"));
            user.setPhone(resultSet.getString("phone"));
            user.setOpenid(resultSet.getString("openid"));
            user.setIdNumber(resultSet.getString("id_number"));
        }
        System.out.println(user);
        //6、资源关闭
        resultSet.close();
        ps.close();
    }
}
